//
//  EXT+UIBarButton.swift
//  新浪微博25
//
//  Created by 李俊扬 on 2017/6/30.
//  Copyright © 2017年 李俊扬. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    convenience init(imgName: String?, title: String?, target: Any?, action: Selector) {
        // 实例化按钮
        let botton = UIButton()
        // 监听事件
        botton.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        
        // 图片
        if let img = imgName {
            botton.setImage(UIImage(named:img), for: UIControlState.normal)
            botton.setImage(UIImage(named:"\(img)_highlighted"), for: UIControlState.highlighted)
        }
        
        // 文字
        if let tit = title {
            
            botton.setTitle(tit, for: UIControlState.normal)
            botton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            botton.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
            botton.setTitleColor(UIColor.orange, for: UIControlState.highlighted)
        }
        
        // 图片大小
        botton.sizeToFit()
        
        // 调用指定构造函数完成实例化
        self.init(customView: botton)
    }
}
